﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.FloatErrorCorrector
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine;

#nullable disable
namespace Netick.Unity;

/// <summary>
/// Handles prediction error correction smoothing for a float.
/// </summary>
[Serializable]
public struct FloatErrorCorrector
{
  public float FloatPreRollback;
  public float FloatError;

  public void Init(float value)
  {
    this.FloatPreRollback = value;
    this.FloatError = 0.0f;
  }

  public void OnPreRollback(float value) => this.FloatPreRollback = value;

  public void OnPostResimulation(float currentValue)
  {
    this.FloatError += this.FloatPreRollback - currentValue;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public void Render(
    ref float interpolatedValue,
    float correctionMultiplier,
    float teleportThreshold,
    float deltaTime)
  {
    this.FloatError *= Mathf.Exp(-correctionMultiplier * deltaTime);
    if ((double) this.FloatError > (double) teleportThreshold)
      this.FloatError = 0.0f;
    interpolatedValue += this.FloatError;
  }
}
